﻿using AutoMapper;
using B.S.AuthCenter.Api.Write.Application.Command.Approval;
using B.S.AuthCenter.Domain.Form;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;

namespace B.S.AuthCenter.Api.Write.Application.Handler.Approval
{
    public class AddAppFormHandler : IRequestHandler<AddAppFormCommand, ApiResult<int>>
    {
        IBaseRepository<AppFormModel> appRepo;
        IMapper mapper;

        public AddAppFormHandler(IBaseRepository<AppFormModel> appRepo, IMapper mapper)
        {
            this.appRepo = appRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(AddAppFormCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();
            var model=mapper.Map<AppFormModel>(request);
            int a=appRepo.Add(model).Result;
            if (a > 0)
            {
                res.Msg = "添加申请成功";
                res.Code=ApiEnum.Success;
                res.Datas= a;
                return Task.FromResult(res);
            }
            else
            {
                res.Msg = "添加申请失败";
                res.Code = ApiEnum.Fail;
                res.Datas = a;
                return Task.FromResult(res);
            }
        }
    }
}
